package com.example.landrentpaymentbackend.controller;

import com.example.landrentpaymentbackend.common.BaseResponse;
import com.example.landrentpaymentbackend.common.ResultUtils;
import com.example.landrentpaymentbackend.dto.ImportResult;
import com.example.landrentpaymentbackend.service.ExcelImportService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
public class ImportExeclController {

    private final ExcelImportService excelImportService;

    /**
     * POST /admin/import
     * 上传 Excel → 解析 → 写入数据库
     */
    @PostMapping("/import")
    public BaseResponse<ImportResult> importExcel(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            // 使用 BaseResponse 直接构造
            return new BaseResponse<>(400, null, "文件为空");
        }
        try {
            ImportResult result = excelImportService.importExcel(file);
            return ResultUtils.success(result);
        } catch (Exception e) {
            return new BaseResponse<>(500, null, "导入失败：" + e.getMessage());
        }
    }
}